<?php

namespace app\controller;

use app\BaseController;
use app\service\CarService;
use app\model\CarModel;
use app\service\XlBillManagementService;
use app\validate\XlBillManagementValidate;
use think\exception\ValidateException;
use \think\facade\Request;
use \think\facade\Db;

/**
 * tms账单管理
 */
class XlBillManagement extends BaseController
{
    /**
     * @desc 单个信息
     * @return void
     */
    public function info()
    {
        $id = Request::param('id/d', 0);
        $uid = Request::param('uid/d', 0);
        $info = XlBillManagementService::info($id);
        returnSuccess(200, '获取成功', $info);
    }

    //列表
    public function lists()
    {
        $uid = Request::param('uid/d', 0);
        $param['uid'] = $uid;
        $param['car_id'] = Request::param('car_id/d',0);
        $param['is_export'] = Request::param('is_export/d',0);
        $param['start_time'] = Request::param('start_time/s', '');
        $param['end_time'] = Request::param('end_time/s', '');
        $param['type'] = Request::param('type/d', 0);
        $param['car_plate_number'] = Request::param('car_plate_number/s', 0);
        $param['page'] = Request::param('page/d', 0);
        $param['limit'] = Request::param('limit/d', 10);
        $logParam['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $logParam['ip'] = Request::ip();
        $data = XlBillManagementService::lists($param);
        if ($data) {
            returnSuccess(200, '获取成功', $data);
        } else {
            returnFail(21101, '获取失败');
        }
    }

    //新增
    public function add_bac()
    {
        $uid = Request::param('uid/d', 0);
        $logParam['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $param['car_plate_number'] = Request::param('car_plate_number/d', 0);
        $param['mileage_recorder'] = Request::param('mileage_recorder/s', "");
        $param['car_plate_number'] = CarModel::info(['id' => $param['car_plate_number']])['car_plate_number'];
        $param['money'] = Request::param('money/s', '');
        $param['type'] = Request::param('type/d', 0);
        $param['pay_type'] = Request::param('pay_type/d', 0);
        $param['fuel_amount'] = Request::param('fuel_amount/s', '');
        $param['description'] = Request::param('description/s', '');
        $param['maintenance_time'] = Request::param('maintenance_time/s', '');
        $param['gas_time'] = Request::param('gas_time/s', '');
        $param['down_start_time'] = Request::param('down_start_time/s', '');
        $param['down_end_time'] = Request::param('down_end_time/s', '');
        $param['repair_img'] = Request::param('repair_img/s', '');
        $param['fuel_img'] = Request::param('fuel_img/s', '');
        $logParam['ip'] = Request::ip();
        if (empty($param['type'])) {
            returnFail(21009, '参数错误');
        }
        if ($param['type'] == 1) {
            $scence = 'gas';
            $param['no'] = getNo('JY');
        } else {
            $scence = 'repair';
            $param['no'] = getNo('WX');
        }
        try {
            validate(XlBillManagementValidate::class)
                ->scene($scence)
                ->check($param);
        } catch (ValidateException $e) {
            returnFail(21009, $e->getError());
        }
        $data = XlBillManagementService::add($param, $logParam);
        if ($data == 10000) {
            returnSuccess(200, '添加成功');
        } else {
            returnFail(21101, '添加失败');
        }
    }

    //新增
    public function add()
    {
        $uid = Request::param('uid/d', 0);
        $logParam['uid'] = $uid;
        $param['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $param['car_id'] = Request::param('car_id/d', 0);
        $param['driver_id'] = Request::param('driver_id/d',0);
        $param['event_location'] = Request::param('event_location/s',"");
        $param['gas_type'] = Request::param('gas_type/d',1);
        $param['fuel_amount'] = Request::param('fuel_amount/s', '');
        $param['repair_img'] = Request::param('repair_img/s', '');
        $param['mileage_recorder'] = Request::param('mileage_recorder/s', "");
        $param['money'] = Request::param('money/s', '');
        $param['type'] = Request::param('type/d', 0);
        $param['pay_type'] = Request::param('pay_type/d', 0);
        $param['mileage_img'] = Request::param('mileage_img/s',"");
        $param['description'] = Request::param('description/s', '');
        $param['maintenance_time'] = Request::param('maintenance_time/s', '');
        $param['maintenance_type'] = Request::param('maintenance_type/d',1);
        $param['gas_time'] = Request::param('gas_time/s', '');
        $param['fuel_img'] = Request::param('fuel_img/s', '');
        $param['remarks'] =  Request::param('remarks/s', '');
        $param['car_plate_number'] =  Request::param('car_plate_number');
        $logParam['ip'] = Request::ip();
        if (empty($param['type'])) {
            returnFail(21009, '参数错误');
        }
        if ($param['type'] == 1) {
            $scence = 'gas';
            $param['no'] = getNo('JY');
        } else {
            $scence = 'repair';
            $param['no'] = getNo('WX');
        }
        try {
            validate(XlBillManagementValidate::class)
                ->scene($scence)
                ->check($param);
        } catch (ValidateException $e) {
            returnFail(21009, $e->getError());
        }
        $data = XlBillManagementService::add($param, $logParam);
        if ($data == 10000) {
            returnSuccess(200, '添加成功');
        } else {
            returnFail(21101, '添加失败');
        }
    }

    //修改1
    public function modify()
    {
        $uid = Request::param('uid/d', 0);
        $id = Request::param('id/d',0);
        $logParam['uid'] = $uid;
        $param['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $param['car_id'] = Request::param('car_id/d', 0);
        $param['driver_id'] = Request::param('driver_id/d',0);
        $param['event_location'] = Request::param('event_location/s',"");
        $param['gas_type'] = Request::param('gas_type/d',1);
        $param['fuel_amount'] = Request::param('fuel_amount/s', '');
        $param['repair_img'] = Request::param('repair_img/s', '');
        $param['mileage_recorder'] = Request::param('mileage_recorder/s', "");
        $param['money'] = Request::param('money/s', '');
        $param['type'] = Request::param('type/d', 0);
        $param['pay_type'] = Request::param('pay_type/d', 0);
        $param['mileage_img'] = Request::param('mileage_img/s',"");
        $param['description'] = Request::param('description/s', '');
        $param['maintenance_time'] = Request::param('maintenance_time/s', '');
        $param['maintenance_type'] = Request::param('maintenance_type/d',1);
        $param['gas_time'] = Request::param('gas_time/s', '');
        $param['fuel_img'] = Request::param('fuel_img/s', '');
        $param['remarks'] =  Request::param('remarks/s', '');
        $param['car_plate_number'] =  Request::param('car_plate_number');
        $logParam['ip'] = Request::ip();
        if (empty($param['type'])) {
            returnFail(21009, '参数错误');
        }
        if ($param['type'] == 1) {
            $scence = 'gas';
        } else {
            $scence = 'repair';
        }
        try {
            validate(XlBillManagementValidate::class)
                ->scene($scence)
                ->check($param);
        } catch (ValidateException $e) {
            returnFail(21009, $e->getError());
        }
        $data = XlBillManagementService::edit($param, $logParam, $id);
        if ($data == 10000) {
            returnSuccess(200, '修改成功');
        } else {
            returnFail(21101, '修改失败');
        }
    }

    //删除
    public function del()
    {
        $uid = Request::param('uid/d', 0);
        $logParam['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $id = Request::param('id/d', 0);

        $logParam['ip'] = Request::ip();

        $data = XlBillManagementService::del($param, $logParam, $id);
        if ($data == 10000) {
            returnSuccess(200, '删除成功');
        } else {
            returnFail(21101, '删除失败');
        }

    }

    //获取车牌列表
    public function getCarNumberList()
    {
        $uid = Request::param('uid/d', 0);
        $param['partner_id'] = get_partner_id($uid);
        $param['uid'] = $uid;
        $data = CarService::concise_list($param);
        returnSuccess(200, '获取成功', $data);
    }


}

